<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" label-width="100px" size="small" :rules="rules">
      <el-form-item label="职务名称:" prop="roleName">
        <el-input v-model="dataForm.roleName" placeholder="请输入职务名称" maxlength="30" clearable></el-input>
      </el-form-item>

      <el-form-item label="职务描述:" prop="roleDesc">
        <el-input v-model="dataForm.roleDesc" type="textarea" placeholder="请输入职务描述" maxlength="300" show-word-limit clearable></el-input>
      </el-form-item>
      <el-form-item style="margin-top: 10px" label-width="130px">
        <el-button type="" @click="close">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          <fa-icon class="fa-check" />保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { CONSTANT_OBJ } from '@/utils/constant'
import { common } from '@/utils/common'
import api from '../api'
export default {
  props: { editData: Object },
  data () {
    return {
      dataForm: {
        roleId: '',
        roleCode: '',
        roleName: '',
        roleDesc: '',
        revision: ''
      },
      rules: {

        roleName: [{ required: true, message: '请输入职务名称', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    if (this.editData && this.editData.roleId) {
      common.listAssign(this.dataForm, this.editData)
    }
  },
  methods: {
    /**
     * 提交代码
     */
    submit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.editData && this.editData.roleId && !this.editData.isCopy) {
            this.editUser()
          } else {
            this.addUser()
          }
        }
      })
    },
    // 新增
    addUser () {
      if (this.editData.isCopy) {
        this.dataForm.revision = ''
        this.copyRole()
      } else {
        api.postAddRoleData(this.dataForm).then(res => {
          const { responseCode, responseMsg } = res
          if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
            this.$message({
              message: `${responseMsg}`,
              type: 'success'
            })
            this.$emit('uppdataSuccess', 'role')
          }
        })
      }
    },
    copyRole () {
      console.log('copy权限', this.dataForm)
      api.copyUserRules(this.dataForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('copy', true)
        }
      })
    },
    // 编辑
    editUser () {
      api.putEditRoleData(this.dataForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess', 'role')
        }
      })
    },
    close () {
      this.$emit('close')
    }
  }
}
</script>
